package org.dromara.common.sendgrid.service;

import org.thymeleaf.TemplateEngine;
import com.sendgrid.Method;
import com.sendgrid.Request;
import com.sendgrid.Response;
import com.sendgrid.SendGrid;
import com.sendgrid.helpers.mail.Mail;
import com.sendgrid.helpers.mail.objects.Content;
import com.sendgrid.helpers.mail.objects.Email;
import com.sendgrid.helpers.mail.objects.Personalization;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.thymeleaf.context.Context;

import java.io.IOException;
import java.util.Map;

@Service
public class SendGridEmailService {
    @Value("${sendgrid.api-key}")
    private String apiKey;

    @Value("${sendgrid.from}")
    private Email from;

    @Autowired
    private TemplateEngine templateEngine;
    public void sendEmail(String toEmail, String subject, String body) {
        Email to = new Email(toEmail);
        Content content = new Content("text/plain", body);
        Mail mail = new Mail(from, subject, to, content);
        Personalization personalization = new Personalization();
        SendGrid sg = new SendGrid(apiKey);
        Request request = new Request();

        try {
            request.setMethod(Method.POST);
            request.setEndpoint("mail/send");
            request.setBody(mail.build());
            Response response = sg.api(request);
            System.out.println(response.getStatusCode());
            System.out.println(response.getBody());
            System.out.println(response.getHeaders());
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public void sendDeliveryDateConfirmEmail(String toEmail, String context){

        String subject = "丹佛斯货期确认 - ";
        String body = context;

        Email to = new Email(toEmail);
        Content mailContent = new Content("text/plain", body);
        Mail mail = new Mail(from, subject, to, mailContent);
        Personalization personalization = new Personalization();
        personalization.addTo(to);
        mail.addPersonalization(personalization);

        SendGrid sg = new SendGrid(apiKey);
        Request request = new Request();
        try {
            request.setMethod(Method.POST);
            request.setEndpoint("mail/send");
            request.setBody(mail.build());
            Response response = sg.api(request);
            System.out.println(response.getStatusCode());
            System.out.println(response.getBody());
            System.out.println(response.getHeaders());
        } catch (IOException ex) {
            ex.printStackTrace();

    }
    }

    public void sendTemplateMailTrial(String to, String subject, Map<String, Object> model) {
        Context context = new Context();
        context.setVariables(model);
        System.out.println(1);
        String html = templateEngine.process("email-template.html", context);
        System.out.println(2);
        System.out.println(html);

        Email toEmail = new Email(to);
        Content content = new Content("text/html", html);
        Mail mail = new Mail(from, subject, toEmail, content);
        SendGrid sg = new SendGrid(apiKey);
        Request request = new Request();
        try {
            request.setMethod(Method.POST);
            request.setEndpoint("mail/send");
            request.setBody(mail.build());
            Response response = sg.api(request);
            System.out.println(response.getStatusCode());
            System.out.println(response.getBody());
            System.out.println(response.getHeaders());
        } catch (IOException ex) {
            ex.printStackTrace();}

    }
}
